package team.yidong.fenda.wx.controller;

import com.qiniu.util.Json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import team.yidong.fenda.entity.Question;
import team.yidong.fenda.entity.User;
import team.yidong.fenda.service.UserService;
import team.yidong.fenda.wx.Service.WxAcceptService;
import team.yidong.fenda.wx.Service.WxHospitalService;
import team.yidong.fenda.wx.Service.WxQuestionService;
import team.yidong.fenda.wx.Service.WxUserService;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xuanlong on 2017/10/11.
 */
@Controller
public class WxQuestionController {
    @Autowired
    WxQuestionService wxQuestionService;
    @Autowired
    WxHospitalService wxHospitalService;
    @Autowired
    WxUserService wxUserService;

    /**
     * 分页返回首页的问题，按时间排序
     *
     * @param id
     * @param page
     * @param page_size
     * @return
     */
    @RequestMapping("/wx/findQuestion")
    @ResponseBody
    public JSONArray findQuestion(@RequestParam Integer id, @RequestParam Integer page, @RequestParam Integer page_size) {

        return wxQuestionService.findWxQustion(id, page, page_size);
    }

    /**
     * 点击问题显示详细内容
     *
     * @param id
     * @return
     */
    @RequestMapping("/wx/findQuestionByIdOrUuName")
    @ResponseBody
    public JSONObject findQuestionById(Integer id, String uuName, String useruuid) {

        return wxQuestionService.findQuestionByIdOrUuName(id, uuName, useruuid);
    }

    /**
     * 点击首页分类显示相关问题列表
     *
     * @param questionClass
     * @return
     */
    @RequestMapping("/wx/findQuestionByClass")
    @ResponseBody
    public JSONArray findQuestionByClass(@RequestParam Integer id, @RequestParam Integer page, @RequestParam Integer page_size, @RequestParam String questionClass) {
        return wxQuestionService.findQuestionByClass(id, page, page_size, questionClass);
    }


    @RequestMapping("/wx/findQuestionByUser")
    @ResponseBody
    public JSONArray findQuestionByUser(@RequestParam String wxNumber) {
        System.out.println("开始查询");
        Jedis jedis = new Jedis("localhost");
        wxNumber = jedis.get(wxNumber);
        JSONArray jsonArray = wxQuestionService.findQuestionByUser(wxNumber);
        return jsonArray;


    }

    /**
     * 添加问题
     *
     * @param info
     * @param wxNumber
     * @param imgPath
     * @param time
     * @param kind
     */
    @RequestMapping(value = "/wx/addQuestion", method = RequestMethod.GET)
    @ResponseBody
    public String addQuestion(@RequestParam String info, @RequestParam String wxNumber, @RequestParam String imgPath, @RequestParam String time, @RequestParam String kind, @RequestParam Integer isOneMaster, Double timeNum, String headImg, String nickName, @RequestParam String isAnonymous, Double askMoney, String out_trade_no) {
        Jedis jedis = new Jedis("localhost");
        wxNumber = jedis.get(wxNumber);
        System.out.println("提问问题时的wx" + wxNumber);
        return wxQuestionService.addQuestion(wxNumber, info, time, imgPath, kind, isOneMaster, timeNum, headImg, nickName, isAnonymous, askMoney, out_trade_no);
    }

    @RequestMapping("/wx/findQuestionByMaster")
    @ResponseBody
    public JSONArray findQuestionByMaster(@RequestParam String openId) {
        Jedis jedis = new Jedis("localhost");
        openId = jedis.get(openId);
        return wxQuestionService.findQuestionByMaster(openId);
    }

    /**
     * 专家端回答过的普通问题
     * @param openId
     * @return
     */
    @RequestMapping("/wx/findQuestionByAnswerMaster")
    @ResponseBody
    public JSONArray findQuestionByAnswerMaster(@RequestParam String openId) {
        Jedis jedis = new Jedis("localhost");
        openId = jedis.get(openId);
        return wxQuestionService.findQuestionByAnswerMaster(openId);
    }

    /**
     * 专家没有回答过的问题，分类
     * @param openId
     * @param questionClass
     * @param choose
     * @param page
     * @param page_size
     * @return
     */
    @RequestMapping("/wx/findByNotAnswerAndQuestionClass")
    @ResponseBody
    public JSONArray findByNotAnswerANdQuestionClass(@RequestParam String openId, String questionClass, @RequestParam String choose, Integer page, Integer page_size) {
        Jedis jedis = new Jedis("localhost");
        openId = jedis.get(openId);
        return wxQuestionService.findByNotAnswerANdQuestionClass(openId, questionClass, choose, page, page_size);

    }

    @RequestMapping("/wx/checkUserIsBlack")
    @ResponseBody
    public String checkUserIsBlack(@RequestParam String wxNumber) {
        Jedis jedis = new Jedis("localhost");
        wxNumber = jedis.get(wxNumber);
        return wxUserService.checkUserIsBlack(wxNumber);
    }

    /**
     * 专家回答过的特问问题
     * @param openid
     * @return
     */
    @RequestMapping("/wx/findTewenQuestion")
    @ResponseBody
    public JSONArray findTewenQuestion(@RequestParam String openid) {
        Jedis jedis = new Jedis("localhost");
        openid = jedis.get(openid);
        return wxQuestionService.findTewenQuestion(openid);
    }
}
